package vsvm.gui.forms;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.FlowLayout;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;

import vsvm.classifier.TestCase;
import vsvm.classifier.MultiTestCase;
import vsvm.gui.VSVMInternalFrame;

public class ClassificationResultsFrame extends VSVMInternalFrame {

	private JPanel pnlContent = null;
	private JPanel pnlTop = null;
	private JButton btnExport = null;
	private JScrollPane jScrollPane = null;
	private JTable tblData = null;

	private ArrayList<TestCase> t;
	
	private void loadData() {
		DefaultTableModel m = new DefaultTableModel(new String[] 
		       {"Index", "Predicted class", "Actual class", "Predicted class probability", "Actual class probability"}, 0);
		for (int i = 0; i < t.size(); ++i) {
			MultiTestCase tc = (MultiTestCase)t.get(i);
                        int mm = 0; int nn = 0; int mn = 0; boolean wasActual = false;
                        for(mm = 0; tc.getPredictedClass() != tc.getLabels()[mm]; mm++){}
                        for(nn = 0; nn < tc.getLabels().length; nn++){
                            if( tc.getActualClass() == tc.getLabels()[nn]){
                                wasActual = true;
                                mn = nn;
                            }
                        }
                        Object[] row = null;
                        if(wasActual){
                            row = new Object[] { i+1, tc.getPredictedClass(),
					tc.getActualClass(), tc.getProbabilities()[mm], tc.getProbabilities()[mn]};
                        } else {
                            row = new Object[] { i+1, tc.getPredictedClass(),
					tc.getActualClass(), tc.getProbabilities()[mm], -1.0};
                        }
			m.addRow(row);
		}
		
		tblData.setModel(m);		
	}
	
	/**
	 * This method initializes 
	 * 
	 */
	public ClassificationResultsFrame(String name, ArrayList<TestCase> tst) {
		super("Results of " + name);
		initialize();
		t = tst;
		
		loadData();
	}

	/**
	 * This method initializes this
	 * 
	 */
	private void initialize() {
        this.setSize(new java.awt.Dimension(480,295));
        this.setContentPane(getPnlContent());
			
	}

	/**
	 * This method initializes pnlContent	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getPnlContent() {
		if (pnlContent == null) {
			pnlContent = new JPanel();
			pnlContent.setLayout(new BorderLayout());
			pnlContent.add(getPnlTop(), java.awt.BorderLayout.NORTH);
			pnlContent.add(getJScrollPane(), java.awt.BorderLayout.CENTER);
		}
		return pnlContent;
	}

	/**
	 * This method initializes pnlTop	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getPnlTop() {
		if (pnlTop == null) {
			FlowLayout flowLayout = new FlowLayout();
			flowLayout.setAlignment(java.awt.FlowLayout.RIGHT);
			flowLayout.setVgap(10);
			flowLayout.setHgap(30);
			pnlTop = new JPanel();
			pnlTop.setLayout(flowLayout);
			pnlTop.setPreferredSize(new java.awt.Dimension(45,45));
			pnlTop.add(getBtnExport(), null);
		}
		return pnlTop;
	}

	/**
	 * This method initializes btnExport	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtnExport() {
		if (btnExport == null) {
			btnExport = new JButton();
			btnExport.setPreferredSize(new java.awt.Dimension(80,25));
			btnExport.setText("Export");
			btnExport.setVisible(false);
		}
		return btnExport;
	}

	/**
	 * This method initializes jScrollPane	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane();
			jScrollPane.setViewportView(getTblData());
		}
		return jScrollPane;
	}

	/**
	 * This method initializes tblData	
	 * 	
	 * @return javax.swing.JTable	
	 */
	private JTable getTblData() {
		if (tblData == null) {
			tblData = new JTable()
			{
				//  Returning the Class of each column will allow different
				//  renderers to be used based on Class
	 
				public Class getColumnClass(int column)
				{
					return getValueAt(0, column).getClass();
				}
	 
				public Component prepareRenderer(
					TableCellRenderer renderer, int row, int column)
				{
					Component c = super.prepareRenderer(renderer, row, column);
	 
					if (!isRowSelected(row))
					{
						Double c1 = (Double)getModel().getValueAt(row, 1);
						Double c2 = (Double)getModel().getValueAt(row, 2);
						c.setBackground(
							c1.equals(c2) ? Color.GREEN : Color.YELLOW );
					}
	 

					return c;
				}
				
				public boolean isCellEditable(int row, int column)
				{
					return false;
				}

			};
		}
		return tblData;
	}

}  //  @jve:decl-index=0:visual-constraint="10,10"
